package cn.tedu.api;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class Test5_File {
    public static void main(String[] args) throws IOException {
        File file = new File("D:\\iotest\\1.txt");
        System.out.println(file.length());
        System.out.println(file.exists());
        System.out.println(file.isFile());
        System.out.println(file.isDirectory());
        System.out.println(file.getName());
        System.out.println(file.getParent());
        System.out.println(file.getAbsolutePath());

        file = new File("D:\\iotest\\2.txt");
        System.out.println(file.createNewFile());
        file = new File("D:\\iotest\\xyz");
        System.out.println(file.mkdir());
        file = new File("D:\\iotest\\a\\b\\c\\d");
        System.out.println(file.mkdirs());
        file = new File("D:\\iotest\\2.txt");
        System.out.println(file.delete());//删除文件或者空文件夹

        //列出文件夹里的资源
        file = new File("D:\\iotest");
        String[] strs = file.list();
        System.out.println(Arrays.toString(strs));

        File[] fs = file.listFiles();
        System.out.println(Arrays.toString(fs));

        //练习
        File f = new File(new Scanner(System.in).nextLine());
        File[] a = f.listFiles();
        System.out.println(Arrays.toString(a));
        long sum = 0;
        for (int i = 0; i < a.length; i++) {
            if (a[i].isDirectory()){
                System.out.println("这是一个文件夹，文件夹名字是："+a[i].getName());
            }else if(a[i].isFile()){
                sum = sum + a[i].length();
            }
        }
        System.out.println(sum);
    }
}
